# -*- coding:utf-8 -*-
import pypyodbc

connStr = r'Driver={Microsoft Access Driver (*.mdb,*.accdb)};PWD=rtmartedp;DBQ=rfdata.dat'
conn = pypyodbc.win_connect_mdb(connStr)
curser = conn.cursor()

def get_by_type(fno)-> tuple:
    # curser = conn.cursor()
    curser.execute('select * from rfitem where FNO=?', (fno,))
    result = curser.fetchall()
    return result

def get_one_by_id(RFcode: str)-> tuple:
    curser.execute('select RFcode, sskb from rfitem where RFcode=?', (RFcode,))
    result = curser.fetchone()
    return result

def get_by_id(rf_id)-> tuple:
    curser.execute('''SELECT TOP 25 * FROM (
        select rfcode, rfsskb, rfzt, rfdate, rftime, rfbmp from rflsz where rfcode=? order by rfdate desc, rftime desc)''', 
        (rf_id,))
    result = curser.fetchall()
    return result

def get_last_18()-> tuple:
    curser.execute('''SELECT TOP 18 * FROM (
            select rfcode, rfsskb, rfzt, rfdate, rftime, rfbmp from rflsz order by rfdate desc, rftime desc)''')
    result = curser.fetchall()
    return result

def get_last_1(RFcode: str)-> tuple:
    curser.execute('''SELECT TOP 1 * FROM (
        select rfzt from rflsz where rfcode=? order by rfdate desc, rftime desc)''', 
        (RFcode,))
    result = curser.fetchone()
    return result

def update(rf_id, name, status, rfdate, rftime, rfbmp)-> None:
    curser.execute('insert into rflsz (rfcode, rfsskb, rfzt, rfdate, rftime, rfbmp) values (?,?,?,?,?,?)',
                   (rf_id, name, status, rfdate, rftime, rfbmp))
    # curser.execute('update rfitem set RFzt=? where RFcode=?', (status, rf_id))
    curser.commit()

def close()-> None:
    conn.close()
